package _dynamic;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @author zk
 * @date 2019-10-27 14:26
 */
public class Hire<T> implements InvocationHandler {
    //被代理对象
    private T obj;

    private Hire(T obj) {
        this.obj = obj;
    }

    public static<T> T getProxy(T obj){
        //arg1:被代理的类加载器，arg2：被代理对象的所有接口，arg3：自定义代理类的对象
        return (T) Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),new Hire(obj));
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("前置工作");
        System.out.println("=============参数=============");
        if(args != null){
            Arrays.stream(args).forEach(System.out::println);
        }
        System.out.println("=============参数=============");

        System.out.println("=============运行=============");
        Object object = method.invoke(obj, args);
        System.out.println("=============运行=============");

        System.out.println("后置工作");
        return object;
    }
}
